package com.home.Common;

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Calendar;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.UIManager;

import java.awt.Point;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


public class Common {
	
	
	
	// lay icon tu duong dan image dua vao
	public static ImageIcon getImageIcon (String path)
	{
		ClassLoader loader = Thread.currentThread().getContextClassLoader();
		URL url = loader.getResource(path);
		
		ImageIcon imgIcon = new ImageIcon(url);
		return imgIcon;
	}
	
	
	
	public static BufferedImage getBufferImage(String path)
	{
		ClassLoader loader = Thread.currentThread().getContextClassLoader();
		URL url = loader.getResource(path);
		
		BufferedImage img = null;
		try {
			img = ImageIO.read(url);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return img;
	}
	
	public static BufferedImage getBufferImage(File file)
	{
		BufferedImage img = null;
		try {
			img = ImageIO.read(file);
		} catch (IOException e) {
			e.printStackTrace();
			img = null;
		}
		return img;
	}
	
	
	public static BufferedImage getImageResized(BufferedImage originalImage, int type,int width,int height)
	{
		BufferedImage resizedImage = new BufferedImage(width, height, type);
		Graphics2D g = resizedImage.createGraphics();
		g.drawImage(originalImage, 0, 0, width, height, null);
		g.dispose();
	 
		return resizedImage;

	}
	
	public static BufferedImage convertImageIconToBufferImg(ImageIcon imgIcon)
	{
		return null;
	}
	
	public static ImageIcon convertBufferImageToImageIcon(BufferedImage imgRe)
	{
		ImageIcon ic = null;
		try {
			Image img = Toolkit.getDefaultToolkit().createImage(imgRe.getSource());
			ic = new ImageIcon(img);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ic;
	}
	
	// viet file anh ra file
	public static void writeImgToFile(BufferedImage img,String type,String path)
	{
		try {
			ImageIO.write(img, type, new File(path));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
//	public static String getNameFileResize(String name)
//	{
//		String n = "";
//		int index = name.indexOf(".");
//		String preName = name.substring(0, index - 1);
//		String subName = name.substring(index);
//		n = preName + "_" +Resources_Duy.SIZE_IMAGE_RESIZE + subName;
//		return n;
//	}
	
	// lay kich thuoc man hinh hien tai
	private static Dimension getScrSize()
	{
		Toolkit toolKit = Toolkit.getDefaultToolkit();
		Dimension scr = toolKit.getScreenSize();
		return scr;
	}
	
	// lay chieu cao man hinh
	public static int getHeight()
	{
		return Common.getScrSize().height;
	}
	
	// lay chieu rong man hinh
	public static int getWidth()
	{
		return Common.getScrSize().width;
	}
	
	// lay id tu thoi gian
	public static long getID()
	{
		return Calendar.getInstance().getTimeInMillis();
	}
	
	// get ten cho hinh
	public static String getNameOfImage(long id,String type)
	{
		return id+type;
	}
	
	// lay duoi cua file
	public static String getExtension(String file)
	{
		String ext = "";
		int index = file.lastIndexOf(".");
		ext += file.substring(index);
		return ext;
	}
	
	// Viet hoa JFileChooser
	public static void tranformJFileChooserToVi()
	{
		UIManager.put("FileChooser.acceptAllFileFilterText", "Tất cả");
		UIManager.put("FileChooser.lookInLabelText", "Tìm trong");
		UIManager.put("FileChooser.cancelButtonText", "Hủy bỏ");
		UIManager.put("FileChooser.cancelButtonToolTipText", "Đóng chọn tập tin");
		UIManager.put("FileChooser.openButtonText", "Mở Tập Tin");
		UIManager.put("FileChooser.openButtonToolTipText", "Mở tập tin đã chọn");
		UIManager.put("FileChooser.filesOfTypeLabelText", "Loại tập tin");
		UIManager.put("FileChooser.fileNameLabelText", "Tên tập tin");
		UIManager.put("FileChooser.listViewButtonToolTipText", "Danh sách"); 
		UIManager.put("FileChooser.listViewButtonAccessibleName", "Danh sách"); 
		UIManager.put("FileChooser.detailsViewButtonToolTipText", "Danh sách chi tiết");
		UIManager.put("FileChooser.detailsViewButtonAccessibleName", "Danh sách chi tiết");
		UIManager.put("FileChooser.upFolderToolTipText", "Trở ra"); 
		UIManager.put("FileChooser.upFolderAccessibleName", "Trở ra"); 
		UIManager.put("FileChooser.homeFolderToolTipText", "Thư mục gốc"); 
		UIManager.put("FileChooser.homeFolderAccessibleName", "Thư mục gốc"); UIManager.put("FileChooser.fileNameHeaderText", "Nome"); 
		UIManager.put("FileChooser.fileSizeHeaderText", "Kích cỡ"); 
		UIManager.put("FileChooser.fileTypeHeaderText", "Loại"); 
		UIManager.put("FileChooser.fileDateHeaderText", "Ngày"); 
		//UIManager.put("FileChooser.fileAttrHeaderText", "Atributos"); 
		UIManager.put("FileChooser.openDialogTitleText","Mở Tập Tin");
		UIManager.put("FileChooser.readOnly", Boolean.TRUE);

	}
	
	public static Rectangle getBoundsChildFrame(int deskTopPaneWidth,int desktopPaneHeight,int width,int height)
	{
		return new Rectangle((deskTopPaneWidth-width)/2,(desktopPaneHeight-height)/2,width,height);
	}
	
	public static Date getDateDefault()
	{
		Date d = new Date(2011, 01, 01);
		return d;
	}
	
	// =================== TRUC SECTION ======================
	
	//phuong thuc tra ve vi tri hien thi cac form thong tin
	public static Point GetBestLocation(Point p, int w, int h)
	{	
		int x,y;
		
		if(h+p.getY() > getHeight())
			y = getHeight()-h;
		else
			y = (int)p.getY();
		
		if(w+p.getX()>getWidth())
			x = getWidth()-w;
		else
			x = (int)p.getX();
		
		return new Point(x, y);
	}
	public static String GetTimeString(Date date, String formatString)
	{
		try { 
		    DateFormat df = new SimpleDateFormat(formatString);  
		    return df.format(date);  
		} catch (Exception e) {
			return "";
		}
	}
	
	
	// =================== END TRUC SECTION ==================
	
}
